#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

#include "good_info.h"

static void menu();

static int init();

static void in();
static void show();
static void search();

static void del();
static void modify();
static void insert();

static void order();
static void total();

static struct commdity comm[50]; // good record array

static int m = 0; // good record number

#define LEN sizeof(struct commdity)
#define FORMAT "%d\t\t%s\t\t%.2lf\t\t%.2lf\t\t%.2lf\n"
#define DATA comm[i].num, comm[i].name, comm[i].price, comm[i].count, comm[i].total
#define FILENAME "data"

void good_info()
{
    menu();

    init();

    int n;
    scanf("%d", &n);
    while (n)
    {
        switch (n)
        {
        case 1:
            in();
            break;
        case 2:
            show();
            break;
        case 3:
            search();
            break;
        case 4:
            del();
            break;
        case 5:
            modify();
            break;
        case 6:
            insert();
            break;
        case 7:
            order();
            break;
        }
        getch();
        system("cls");
        menu();
        scanf("%d", &n);
    }
    //    remove(FILENAME);
}

static void menu()
{
    printf("|*****************good manager system*****************|\n");
    printf("|**\n");
    printf("|**\n");
    printf("|**0.quit\n");
    printf("|**1.input\n");
    printf("|**2.view\n");
    printf("|**3.search\n");
    printf("|**4.delete\n");
    printf("|**5.modify\n");
    printf("|**6.insert\n");
    printf("|**7.order\n");
    printf("|**\n");
    printf("|**\n");
    printf("|******************good manager system*****************|\n");
    printf("api\n");
    printf("  fopen fwrite fread fclose\n");
    printf("  strcmp\n");
    printf("learn -> array insert delete.\n");
    printf("select num(0-7).\n");
    printf("|------------------------------------------------------|\n");
}

/* good init */
static int init()
{
    FILE *fp;
    if ((fp = fopen(FILENAME, "ab+")) == NULL)
    {
        printf("fail to open.\n");
        return m;
    }
    while (!feof(fp))
    {
        if (fread(&comm[m], LEN, 1, fp) == 1)
            m++;
    }
    fclose(fp);
    return m;
}

/* good input */
static void in()
{
    int i;
    char ch[2];
    FILE *fp;

    show();

    if ((fp = fopen(FILENAME, "ab")) == NULL)
    {
        printf("fail to open.\n");
        return;
    }
    for (i = 0; i < m; i++)
        fwrite(&comm[i], LEN, 1, fp);
    printf("select yes or no?(y/n):");
    scanf("%s", ch);
    while (strcmp(ch, "Y") == 0 || strcmp(ch, "y") == 0)
    {
        printf("input good no:");
        scanf("%d", &comm[m].num);
        for (i = 0; i < m; i++)
            if (comm[i].num == comm[m].num)
            {
                printf("good exists.\n");
                //                getch();
                fclose(fp);
                return;
            }

        printf("input good name:");
        scanf("%s", comm[m].name);

        printf("input good price:");
        scanf("%lf", &comm[m].price);

        printf("input good count:");
        scanf("%lf", &comm[m].count);

        comm[m].total = comm[m].price * comm[m].count;

        if (fwrite(&comm[m], LEN, 1, fp) != 1)
        {
            printf("fail to write.\n");
            //            getch();
        }
        else
        {
            printf("%s input success.\n", comm[m].name);
            m++;
        }
        printf("select yes or no?(y/n):");
        scanf("%s", ch);
    }
    fclose(fp);
    printf("OK!\n");
}

/* good view */
static void show()
{
    if (m > 0)
    {
        system("cls");
        int i;
        printf("NO\t\tNAME\t\tPRICE\t\tCOUNT\t\tTOTAL\n");
        for (i = 0; i < m; i++)
        {
            printf(FORMAT, DATA);
        }
        total();
    }
    else
    {
        printf("No goods.\n");
    }
}

/* good search */
static void search()
{
    if (m > 0)
    {
        printf("Please enter the item no you want to search:");
        int n, i;
        scanf("%d", &n);
        for (i = 0; i < m; i++)
        {
            if (n == comm[i].num)
            {
                printf("NO\t\tNAME\t\tPRICE\t\tCOUNT\t\tTOTAL\n");
                printf(FORMAT, DATA);
                break;
            }
        }
        if (i == m)
            printf("Not found.\n");
    }
    else
    {
        printf("No goods.\n");
    }
}

/* delete */
static void del()
{
    if (m > 0)
    {
        printf("Please enter the item no you want to delete:");
        int n, i;
        scanf("%d", &n);
        for (i = 0; i < m; i++)
        {
            if (n == comm[i].num)
            {
                int j;
                for (j = i; j < m; j++)
                {
                    comm[j] = comm[j + 1];
                }

                FILE *fp;
                if ((fp = fopen(FILENAME, "wb")) == NULL)
                {
                    printf("fail to open.\n");
                    return;
                }
                for (j = 0; j < m - 1; j++)
                    if (fwrite(&comm[j], LEN, 1, fp) != 1)
                    {
                        printf("fail to write.\n");
                        //                        getch();
                    }
                fclose(fp);

                m--;
                printf("deleted item, no:[%d]", n);
                break;
            }
        }
        if (i == m)
            printf("Not found item.\n");
    }
    else
    {
        printf("No goods.\n");
    }
}

/* modify */
static void modify()
{
    if (m > 0)
    {
        printf("please enter the item no you want to modify:");
        int n, i;
        scanf("%d", &n);
        for (i = 0; i < m; i++)
        {
            if (n == comm[i].num)
            {

                printf("good name:%s\n", comm[i].name);
                scanf("%s", comm[i].name);

                printf("good price:%.2lf\n", comm[i].price);
                scanf("%lf", &comm[i].price);

                printf("good count:%.2lf\n", comm[i].count);
                scanf("%lf", &comm[i].count);

                comm[i].total = comm[i].price * comm[i].count;

                FILE *fp;
                if ((fp = fopen(FILENAME, "wb")) == NULL)
                {
                    printf("fail to open.\n");
                    return;
                }
                int j;
                for (j = 0; j < m; j++)
                    if (fwrite(&comm[j], LEN, 1, fp) != 1)
                    {
                        printf("fail to write.\n");
                        //                        getch();
                    }
                fclose(fp);

                printf("have bean modified");
                break;
            }
        }
        if (i == m)
            printf("Not found item.\n");
    }
    else
    {
        printf("No goods.\n");
    }
}

/* insert */
static void insert()
{
    printf("please enter the item index you want to insert:");
    int n, i, j;
    scanf("%d", &n);
    if (n >= m)
    {
        printf("please enter the item no:");
        int k;
        scanf("%d", &k);
        for (j = 0; j < m; j++)
            if (comm[j].num == k)
            {
                printf("no already exists.\n");
                //                getch();
                return;
            }

        comm[m].num = k;

        printf("please enter the good name:");
        scanf("%s", comm[m].name);

        printf("please enter the good price:");
        scanf("%lf", &comm[m].price);

        printf("please enter the good count:");
        scanf("%lf", &comm[m].count);

        comm[m].total = comm[m].price * comm[m].count;
    }
    else
    {
        for (i = 0; i < m; i++)
            if (i == n)
            {

                printf("please enter the item no:");
                int k;
                scanf("%d", &k);
                for (j = 0; j < m; j++)
                    if (comm[j].num == k)
                    {
                        printf("no already exists.\n");
                        //                        getch();
                        return;
                    }

                for (j = m - 1; j >= i; j--)
                //                for(j = i; j < m; j++)
                {
                    comm[j + 1] = comm[j];
                }

                comm[i].num = k;

                printf("please enter the good name:");
                scanf("%s", comm[i].name);

                printf("please enter the good price:");
                scanf("%lf", &comm[i].price);

                printf("please enter the good count:");
                scanf("%lf", &comm[i].count);

                comm[i].total = comm[i].price * comm[i].count;

                break;
            }
    }

    FILE *fp;
    if ((fp = fopen(FILENAME, "wb")) == NULL)
    {
        printf("fail to open.\n");
        return;
    }
    for (j = 0; j < m + 1; j++)
        if (fwrite(&comm[j], LEN, 1, fp) != 1)
        {
            printf("fail to write.\n");
            //            getch();
        }
    fclose(fp);

    m++;
}

static void order()
{
    int i, j;
    struct commdity tmp;
    for (i = 0; i < m; i++)
    {
        for (j = i + 1; j < m; j++)
        {
            if (comm[i].num > comm[j].num)
            {
                tmp = comm[i];
                comm[i] = comm[j];
                comm[j] = tmp;
            }
        }
    }

    FILE *fp;
    if ((fp = fopen(FILENAME, "wb")) == NULL)
    {
        printf("fail to open.\n");
        return;
    }
    for (j = 0; j < m; j++)
        if (fwrite(&comm[j], LEN, 1, fp) != 1)
        {
            printf("fail to write.\n");
            //            getch();
        }
    fclose(fp);

    show();
}

static void total()
{
    printf("\ntotal:%d\n", m);
}

void main_menu()
{
    printf("*************system select*************\n");
    printf("** 0.quit\n");
    printf("** 1.good manager system\n");
    printf("*************system select*************\n");
}

void good_main()
{
    main_menu();

    int n;
    scanf("%d", &n);
    while (n)
    {
        switch (n)
        {
        case 1:
            system("cls");
            good_info();
            break;
        }
        //        getch();
        system("cls");
        main_menu();
        scanf("%d", &n);
    }
}